package com.renwei.yunlong.adapter;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.renwei.yunlong.R;
import com.renwei.yunlong.YunLongApplication;
import com.renwei.yunlong.activity.work.PhotoDisplayActivity;
import com.renwei.yunlong.bean.CertificateInfoBean;
import com.renwei.yunlong.utils.LogUtil;
import com.renwei.yunlong.http.Api;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Project :  yunaandroid.
 * Package name: com.renwei.yunlong.adapter
 * Created by :  Benjamin.
 * Created time: 2017/10/23 16:38
 * Changed by :  Benjamin.
 * Changed time: 2017/10/23 16:38
 * Class description:
 */

public class UserCertificateListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private String companyType = "";
    private Context context;
    private List<CertificateInfoBean.RowsBean> certificateList;
    private String from = "info";


    private onCertificateClickListener onClick;

    public interface onCertificateClickListener {
        void clickEdit();

        void showPop(View view, String fileId, String attachPath);

    }

    public UserCertificateListAdapter(Context context) {
        this.context = context;
    }


    public void setOnCertificateListener(onCertificateClickListener onClick) {
        this.onClick = onClick;
    }

    public void refresh(List<CertificateInfoBean.RowsBean> certificateList, String from, String companyType) {
        this.certificateList = certificateList;
        this.from = from;
        this.companyType = companyType;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_user_certificate, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof ViewHolder) {
            String imageUrl = "";
            LogUtil.e("UserCertificateListAdapter ---CompanyType" + YunLongApplication.getCompanyType());
            if ("1".equals(companyType)) {
                imageUrl = String.valueOf(Api.$().OWNER_FILE_URL + "/" + certificateList.get(position).getAttachPath());
            } else {
                imageUrl = String.valueOf(Api.$().SERVICE_FILE_URL + "/" + certificateList.get(position).getAttachPath());
            }
            LogUtil.e("图片地址：" + imageUrl);
            Glide.with(context).load(imageUrl).placeholder(R.mipmap.icon_img_blank)
                    .error(R.mipmap.icon_img_blank).into(((ViewHolder) holder).ivCertificateImage);
            ((ViewHolder) holder).tvCertificateName.setText(certificateList.get(position).getFilename());
            ((ViewHolder) holder).tvCertificateLevel.setText(certificateList.get(position).getQualName());
            if ("friend".equals(from)) {
                ((ViewHolder) holder).ivCertificateMore.setVisibility(View.GONE);
            } else {
                ((ViewHolder) holder).ivCertificateMore.setVisibility(View.VISIBLE);
            }
            final String finalImageUrl = imageUrl;
            ((ViewHolder) holder).cardview.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ArrayList<String> arrayList = new ArrayList<>();
                    arrayList.add(finalImageUrl);
                    PhotoDisplayActivity.OpenActivity(context, arrayList, 0);
                }
            });
            ((ViewHolder) holder).ivCertificateMore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onClick.showPop(view, String.valueOf(certificateList.get(position).getId()), String.valueOf(certificateList.get(position).getAttachPath()));
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        int count = certificateList == null ? 0 : certificateList.size();
        return count;
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_certificate_name)
        TextView tvCertificateName;
        @Bind(R.id.iv_certificate_more)
        ImageView ivCertificateMore;
        @Bind(R.id.user_certificate_top)
        RelativeLayout userCertificateTop;
        @Bind(R.id.tv_certificate_level)
        TextView tvCertificateLevel;
        @Bind(R.id.iv_certificate_image)
        ImageView ivCertificateImage;
        @Bind(R.id.cardview)
        CardView cardview;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
